package com.ruoyi.StarStudio.Controller;

import com.github.pagehelper.PageInfo;
import com.ruoyi.StarStudio.Entity.*;
import com.ruoyi.StarStudio.Service.GoodsService;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.PageUtils;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/goods")
public class GoodsController  {

    @Autowired
    GoodsService goodsService;

    /**
     * 条件查询 商品分页接口
     * @param goods 商品对象
     * @return 分页结果对象 PageResult
     */
    @GetMapping("/page")
    public PageResult page(Goods goods){
        PageUtils.startPage();
        List<Goods> goodsList;
        long allRecordNumber;
        try {
            goodsList = goodsService.findGoodsList(goods);
            allRecordNumber = goodsService.findAllRecordNumber();
        } catch (Exception e) {
            e.printStackTrace();
            return new PageResult(HttpStatusCode.ERROR, MessageConstant.QUERY_PAGE_FAIL);
        }
        return new PageResult(HttpStatusCode.SUCCESS,MessageConstant.QUERY_PAGE_SUCCESS, new PageInfo<>(goodsList).getTotal(), goodsList, allRecordNumber);
    }

    /**
     * 获取商品简要信息集合
     * @return 结果对象 Result
     */
    @GetMapping("/choose_info")
    public Result getGoodsChooseResultList(){
        List<GoodsChooseResult> goodsChooseResults;
        try {
            goodsChooseResults =  goodsService.getGoodsChooseResultList();
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR,MessageConstant.GET_GOODS_INFO_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS,MessageConstant.GET_GOODS_INFO_SUCCESS,goodsChooseResults);
    }

    /**
     * 根据商品ID查找商品
     * @param id 商品ID
     * @return 结果对象 Result
     */
    @GetMapping("/{id}")
    public Result findGoodsById(@PathVariable long id){
        Goods byGoodsId;
        try {
             byGoodsId = goodsService.findByGoods_id(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR,MessageConstant.QUERY_GOODS_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS,MessageConstant.QUERY_GOODS_SUCCESS,byGoodsId);
    }

    /**
     * 根据商品个体ID查询对应商品信息
     * @param goods_item_id 商品个体ID
     * @return 结果对象
     */
    @GetMapping("/by_item_id/{goods_item_id}")
    public Result findGoodsByGoodsItemId(@PathVariable long goods_item_id){
        Goods byGoods_item_id;
        try {
            byGoods_item_id = goodsService.findByGoods_Item_Id(goods_item_id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR,MessageConstant.QUERY_GOODS_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS,MessageConstant.QUERY_GOODS_SUCCESS,byGoods_item_id);
    }

    /**
     * 导出商品信息到 Excel表格
     * @param response http响应
     * @param goods 商品对象
     */
    @Log(title = "商品管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Goods goods) {
        List<Goods> list = goodsService.findGoodsList(goods);
        ExcelUtil<Goods> util = new ExcelUtil<>(Goods.class);
        util.exportExcel(response, list, "商品数据");
    }

    /**
     * 根据商品ID删除商品（可单个删除也可批量删除）
     * @param Ids 删除的商品ID
     * @return 结果对象 Result
     */
    @Log(title = "商品管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{Ids}")
    public Result deleteGoods(@PathVariable String Ids){
        try {
            if (Ids.contains(",")){
                String[] Id = Ids.split(",");
                for (String IdStr : Id) {
                    goodsService.DeleteGoodsById(Integer.parseInt(IdStr));
                }
            }else {
                goodsService.DeleteGoodsById(Integer.parseInt(Ids));
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR,MessageConstant.DELETE_GOODS_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS,MessageConstant.DELETE_GOODS_SUCCESS);
    }

    /**
     * 商品概览图片上传
     * @param file 文件对象
     * @return 结果对象 Result
     * @throws IOException IO异常
     */
    @Log(title = "商品管理", businessType = BusinessType.IMPORT)
    @PostMapping("/avatar")
    public Result avatar(@RequestParam("avatarFile") MultipartFile file) throws IOException {
        if (!file.isEmpty()) {
            String avatar = FileUploadUtils.upload(RuoYiConfig.getAvatarPath(), file); //路径 ->  /home/ruoyi/uploadPath/avatar
            return new Result(HttpStatusCode.SUCCESS, MessageConstant.UPLOAD_IMAGE_SUCCESS, avatar);
        }
        return new Result(HttpStatusCode.ERROR, MessageConstant.UPLOAD_IMAGE_FAIL);
    }

    /**
     * 新增商品
     * @param goods 商品对象
     * @return 结果对象 Result
     */
    @Log(title = "商品管理", businessType = BusinessType.INSERT)
    @PostMapping
    public Result addGoods(@RequestBody Goods goods){
        try {
            goodsService.addGoods(goods);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR,MessageConstant.ADD_GOODS_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS,MessageConstant.ADD_GOODS_SUCCESS);
    }

    /**
     * 修改商品
     * @param goods 商品对象
     * @return 结果对象 Result
     */
    @Log(title = "商品管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result updateGoods(@RequestBody Goods goods){
        try {
            goodsService.updateGoods(goods);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(HttpStatusCode.ERROR,MessageConstant.UPDATE_GOODS_FAIL);
        }
        return new Result(HttpStatusCode.SUCCESS,MessageConstant.UPDATE_GOODS_SUCCESS);
    }
}
